package com.weareint.biz.controller;

import com.weareint.biz.vo.TUserVo;
import com.weareint.biz.search.TUserSearch;
import com.weareint.biz.service.ITUserService;
import com.weareint.common.jpabase.ResultVoUtil;
import com.weareint.common.jpabase.ResultVo;
import com.weareint.common.jpabase.PageVo;
import com.weareint.common.jpabase.PageUtil;
import org.springframework.data.domain.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <pre>
 *       前端控制器
 * </pre>
 *
 * @author Johnny
 * @date 2024-04-20
 */
@Api(tags = "")
@RestController
@Slf4j
@RequestMapping("/api/tUser")
public class TUserController {

    private final ITUserService tUserService;

    public TUserController(ITUserService tUserService) {
        this.tUserService = tUserService;
    }

    /**
     * 带条件查询的 分页接口
     *
     * @param tUserSearch : 查询条件
     * @param pageVo : 分页条件
     * @return : ResultVo<Page<TUserVo>>
     */
    @GetMapping("/listByCondition")
    @ApiOperation(value = "根据条件查询 (分页)")
    public ResultVo<Page<TUserVo>> listByCondition(TUserSearch tUserSearch,
                                                      PageVo pageVo) {
        Page<TUserVo> page = tUserService.listByCondition(tUserSearch, PageUtil.initPage(pageVo));
        return ResultVoUtil.success(page);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增信息")
    public ResultVo<Void> add(@RequestBody @Valid TUserVo tUserVo) {
        tUserService.add(tUserVo);
        return ResultVoUtil.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改信息")
    public ResultVo<Void> edit(@RequestBody @Valid TUserVo tUserVo) {
        tUserService.edit(tUserVo);
        return ResultVoUtil.success();
    }

    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", example = "1" , dataTypeClass = Long.class)
    public ResultVo<Void> delete(@PathVariable Long id) {
        tUserService.delete(id);
        return ResultVoUtil.success();
    }

    @PostMapping("/deletes")
    @ApiOperation(value = "批量删除信息")
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "Long[]" , dataTypeClass = Arrays.class)
    public ResultVo<Void> deletes(Long[] ids) {
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        tUserService.deletes(idList);
        return ResultVoUtil.success();
    }

}
